package igloo.ui;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.swingx.JXHyperlink;
import org.jdesktop.swingx.JXPanel;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;

@SuppressWarnings("serial")
public class LoginPanel extends JXPanel
{
    private JLabel mAccessKeyLabel;
    private JTextField mAccessKeyTextField;

    private JLabel mSecretKeyLabel;
    private JPasswordField mSecretKeyPasswordField;

    private JButton mLoginButton;
    private JCheckBox mSaveCredentialsCheckBox;
    private JXHyperlink mHyperlink;

    private Callback mCallback;

    public LoginPanel()
    {
        this(null);
    }

    public LoginPanel(Callback callback)
    {
        mCallback = callback;

        setBackground(Color.WHITE);
        setLayout(new MigLayout("fillx, insets 25 20 25 20"));

        add(getAccessKeyLabel(), "wrap");
        add(getAccessKeyTextField(), "h 22!, w 250!, span, growx, wrap");
        add(getSecretKeyLabel(), "wrap");
        add(getSecretKeyPasswordField(), "h 22!, w 250!, span, growx, wrap");
        add(getSaveCredentialsCheckBox(), "gaptop 8, wrap");
        add(getLoginButton(), "growx, align center, gaptop 10, wrap");
        add(getHyperlinkButton(), "growx, gaptop 15");
    }

    public void setCallback(Callback callback)
    {
        mCallback = callback;
    }

    public AWSCredentials getCredentials()
    {
        char[] key = getSecretKeyPasswordField().getPassword();
        AWSCredentials creds = new BasicAWSCredentials(getAccessKeyTextField().getText(),
                                                       new String(key));

        for(int i = 0; i < key.length; ++i)
            key[i] = 0;

        return creds;
    }

    private JLabel getAccessKeyLabel()
    {
        if(mAccessKeyLabel == null)
        {
            mAccessKeyLabel = new JLabel("Access Key:");
        }

        return mAccessKeyLabel;
    }

    private JTextField getAccessKeyTextField()
    {
        if(mAccessKeyTextField == null)
        {
            mAccessKeyTextField = new JTextField();
            mAccessKeyTextField.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        }

        return mAccessKeyTextField;
    }

    private JLabel getSecretKeyLabel()
    {
        if(mSecretKeyLabel == null)
        {
            mSecretKeyLabel = new JLabel("Secret Key:");
        }

        return mSecretKeyLabel;
    }

    private JPasswordField getSecretKeyPasswordField()
    {
        if(mSecretKeyPasswordField == null)
        {
            mSecretKeyPasswordField = new JPasswordField();
            mSecretKeyPasswordField.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        }

        return mSecretKeyPasswordField;
    }

    private JButton getLoginButton()
    {
        if(mLoginButton == null)
        {
            mLoginButton = new JButton("Login");
            mLoginButton.setEnabled(false);

            mLoginButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    if(mCallback != null)
                    {
                        mCallback.login(getAccessKeyTextField().getText(),
                                        getSecretKeyPasswordField().getPassword());
                    }
                }
            });

            KeyListener listener = new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent e)
                {
                    mLoginButton.setEnabled(!getAccessKeyTextField().getText().isEmpty()
                            && getSecretKeyPasswordField().getPassword().length != 0);
                }
            };

            getAccessKeyTextField().addKeyListener(listener);
            getSecretKeyPasswordField().addKeyListener(listener);
        }

        return mLoginButton;
    }

    private JCheckBox getSaveCredentialsCheckBox()
    {
        if(mSaveCredentialsCheckBox == null)
        {
            mSaveCredentialsCheckBox = new JCheckBox("Save Credentials");
            mSaveCredentialsCheckBox.setOpaque(false);
            mSaveCredentialsCheckBox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {

                }
            });
        }

        return mSaveCredentialsCheckBox;
    }

    private JXHyperlink getHyperlinkButton()
    {
        if(mHyperlink == null)
        {
            mHyperlink = new JXHyperlink();
            mHyperlink.setHorizontalAlignment(JXHyperlink.CENTER);
            try
            {
                mHyperlink.setURI(new URI("http://aws.amazon.com/"));
            }
            catch(URISyntaxException e)
            {
                e.printStackTrace();
            }
            mHyperlink.setText("Find Your Keys on AWS");
        }

        return mHyperlink;
    }

    private void openUrl(String url)
    {
        try
        {
            Desktop desktop = Desktop.getDesktop();
            if(desktop.isSupported(Action.BROWSE))
            {
                URI uri = new URI(url);
                desktop.browse(uri);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public interface Callback
    {
        public void login(String accessKey, char[] secretKey);
    }
}
